//
//  QDPaymentSignManager.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/6.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

protocol QDPaymentSignManagerDelegate: class {
    
    func complete(payType: QDPayType, data: String,id: String)
    func fail(error: String)
}

typealias QDPaySignType = QDPayType


/// 签约支付的管理者
class QDPaymentSignManager: NSObject {

    /// 单例对象
    static let sharedInstance = QDPaymentSignManager()
    /// 代理对象
    var delegate: QDPaymentSignManagerDelegate?
    /// 支付方式
    var type: QDPayType = .ALiPay
    /// 用于展示失败的controller
    weak var viewController: UIViewController?
    
    /// 微信
    private let wxManager = QDPaymentSignWeixinManager.sharedInstance
    /// 支付宝
    private let aliManager = QDPaymentSignAliManager.sharedInstance
    /// 银联
    private let unionManager = QDPaymentSignUnionManager.sharedInstance
    
    
    private override init(){
        super.init()
        wxManager.delegate = self
        aliManager.delegate = self
        unionManager.delegate = self
    }
    
    /// 进行支付
    ///
    /// - Parameters:
    ///   - type: 支付类型
    ///   - data: 支付需要的数据
    ///   - delegate: 代理对象
    func sign(type: QDPayType,data: [AnyHashable : Any], delegate: QDPaymentSignManagerDelegate? = nil,viewController: UIViewController?){
        
        self.delegate = delegate
        self.type = type
        
        switch type {
        case .Weixin: wxManager.sign(data: data, viewController: viewController)
        case .ALiPay: aliManager.sign(data: data, viewController: viewController)
        case .Union: unionManager.sign(data: data, viewController: viewController)
        default: break
        }
    }
}


extension QDPaymentSignManager: QDPaymentSignManagerDelegate {
    
    func complete(payType: QDPayType, data: String, id: String) {
        delegate?.complete(payType: payType, data: data, id: id)
    }
    
    func fail(error: String) {
    
    }
}




//MARK: 签约
protocol QDSubSignManager {
    func sign(data: [AnyHashable : Any],viewController: UIViewController?)
}



/// 微信签约的管理
final class QDPaymentSignWeixinManager: NSObject,QDSubSignManager {
    
    @objc(sharedInstance)
    static let sharedInstance = QDPaymentSignWeixinManager()
    fileprivate var delegate: QDPaymentSignManagerDelegate?
    
    private override init() {
        super.init()
    }
    
    func sign(data: [AnyHashable : Any], viewController: UIViewController?) {
        print("微信签约啦!")
        guard let viewController = viewController else {//保证存在viewController
            return
        }
        
        //请求微信签约
        QDNetworkingManager.requestPaySign(type: .Weixin, data: ["payChannel": QDPaySignType.Weixin.rawValue], complete: { (dataString,id) in
            
            self.delegate?.complete(payType: .Weixin, data: dataString, id: id)
            
        }) { (msg, errorType) in
            viewController.view.showTextHUD(text: msg)
        }
    }
}




/// 支付宝签约的管理者
final class QDPaymentSignAliManager: NSObject,QDSubSignManager {
    
    /// 接收通知
//    private let PaymentAliResultNoticationName = "PaymentAliResultNoticationName"
    
    static let sharedInstance = QDPaymentSignAliManager()
    fileprivate var delegate: QDPaymentSignManagerDelegate?
    
    private override init() {
        super.init()
    }
    
    func sign(data: [AnyHashable : Any], viewController: UIViewController?) {
        
        //请求支付宝签约
        QDNetworkingManager.requestPaySign(type: .ALiPay, data: ["payChannel": QDPaySignType.ALiPay.rawValue], complete: { (dataString,id) in
            
            self.delegate?.complete(payType: .ALiPay, data: dataString, id: id)
            
        }) { (msg, errorType) in
            
            viewController?.view.showTextHUD(text: msg)
        }
    }

}


/// 银联支付的管理者
final class QDPaymentSignUnionManager: NSObject,QDSubSignManager {
    
    /// 接收通知
//    private let PaymentUnionResultNoticationName = "PaymentUnionResultNoticationName"
    
    static let sharedInstance = QDPaymentSignUnionManager()
    fileprivate var delegate: QDPaymentSignManagerDelegate?
    
    private override init() {
        super.init()
        
//        NotificationCenter.default.addObserver(self, selector: #selector(unionNoticationDidReceive(notification:)), name: NSNotification.Name(rawValue: PaymentUnionResultNoticationName), object: UPPaymentControl.default())
    }
    
    func sign(data: [AnyHashable : Any], viewController: UIViewController?) {
        print("银联签约啦!")
        
        guard let viewController = viewController else {
            return
        }
        
        viewController.view.showTextHUD(text: "实名信息验证中..",delayHidden: 10)
        
        //请求银联签约
        QDNetworkingManager.requestPaySign(type: .Union, data: data as! [String : String], complete: { (dataString,id) in
            
            self.delegate?.complete(payType: .Union, data: dataString, id: id)
            
        }) { (msg, errorType) in
            
            viewController.view.showTextHUD(text: msg)
        }
    }
}




